// import { defineConfig } from "@mikro-orm/mariadb";
// import { ReflectMetadataProvider, defineConfig } from "@mikro-orm/mysql";
import { ReflectMetadataProvider, defineConfig } from '@mikro-orm/mariadb';
import { EntityGenerator } from '@mikro-orm/entity-generator';
import dotenv from 'dotenv-flow';
import { Author } from '@/entities/Author';

import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
import { loadSecrets } from '@/utils/back/secret';

dotenv.config({
  path: './env',
});

let { MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT } =
  process.env;

console.log('🚀 ~ process.env.NODE_ENV:', process.env.NODE_ENV);
// console.log('🚀 ~ process.env.DB_ENV:', process.env.DB_ENV);
// if (["production", "development"].includes(process.env.NODE_ENV)) {
if (process.env.DB_ENV !== 'local') {
  const secretData = loadSecrets();
  MYSQL_HOST = secretData.MYSQL.HOST;
  MYSQL_USER = secretData.MYSQL.USERNAME;
  MYSQL_PASSWORD = secretData.MYSQL.PASSWORD;
  MYSQL_DATABASE = secretData.MYSQL.DATABASE;
  MYSQL_PORT = secretData.MYSQL.PORT;
}

export default defineConfig({
  // It is important that entities will point to the compiled JS files,
  // and entitiesTs will point to the TS source files. You should not mix those.

  // !!!! 注意 在nextjs中使用 micro-orm, entities的指定需要是 类对象，
  // !!! 不能指定路径，否则会提示  SyntaxError: Cannot use import statement outside a module
  // entities: [Author],
  // entitiesTs: [Author],

  // entities: [".next/server/chunks/*.js"],
  // entities: [Author],
  entities: [],

  // default since v4, so not needed to specify explicitly
  // metadataProvider: ReflectMetadataProvider,
  // metadataProvider: TsMorphMetadataProvider,

  host: MYSQL_HOST,
  port: Number(MYSQL_PORT),
  user: MYSQL_USER || 'root',
  password: MYSQL_PASSWORD || 'root',
  dbName: MYSQL_DATABASE,

  // type: 'mariadb',

  allowGlobalContext: true,

  // extensions: [EntityGenerator],

  debug: process.env.NODE_ENV !== 'production',
});
